//
//  AddJobIntensionVC.m
//  MyProject
//
//  Created by 石小强 on 2018/3/20.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "AddJobIntensionVC.h"
#import "PositionSelectViewController.h"
#import "SalarySelectViewController.h"
#import "ExpectedIndustryVC.h"
#import "CityModel.h"
#import "SelectCityViewController.h"

@interface AddJobIntensionVC ()

@property (nonatomic, copy) NSString *minSalary;
@property (nonatomic, copy) NSString *maxSalary;
@property (nonatomic, strong) CityModel *selectCityModel;
@property (nonatomic, strong) JobModel *selectJobModel;
@property (nonatomic, strong) NSArray *industries;

@end

@implementation AddJobIntensionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.index == 0) {
        self.navigationItem.title = @"新建求职意向";
        self.addCompleteBtn.hidden = NO;
        self.editDeleteBtn.hidden = YES;
        self.editComplteBtn.hidden = YES;
    } else {
        self.navigationItem.title = @"编辑求职意向";
        self.addCompleteBtn.hidden = YES;
        self.editDeleteBtn.hidden = NO;
        self.editComplteBtn.hidden = NO;
        
        self.jobL.text = self.model.jobName;
        self.industryL.text = [NSString stringWithFormat:@"%zd个标签", self.model.industries.count];
        self.addressL.text = self.model.regionName;
        self.salaryL.text = [NSString stringWithFormat:@"%@k-%@k", self.model.minIntentionalSalary, self.model.maxIntentionalSalary];
        self.industries = self.model.industries;
        
        self.minSalary = self.model.minIntentionalSalary;
        self.maxSalary = self.model.maxIntentionalSalary;
        JobModel *job = [[JobModel alloc] init];
        job.jobId = self.model.jobId;
        job.jobName = self.model.jobName;
        self.selectJobModel = job;
        
        CityModel *city = [[CityModel alloc] init];
        city.regionId = self.model.region;
        city.regionName = self.model.regionName;
        self.selectCityModel = city;
        
        self.industries = self.model.industries;
    }
    self.view.backgroundColor = kStyleColor;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SearchCityBlock:) name:@"searchcity" object:nil];
}

- (BOOL)checkSelect {
    if (!self.selectJobModel) {
        [CommonUtils showToast:@"请选择期望职位"];
        return NO;
    }
    if (!self.industries || self.industries.count == 0) {
        [CommonUtils showToast:@"请选择期望行业"];
        return NO;
    }
    if (!self.selectCityModel) {
        [CommonUtils showToast:@"请选择工作地点"];
        return NO;
    }
    if (!self.minSalary) {
        [CommonUtils showToast:@"请选择薪资要求"];
        return NO;
    }
    return YES;
}

- (IBAction)addCompleteBtnClick:(id)sender {
    if ([self checkSelect]) {
        WS(weakSelf)
        [self showProgressWithView:self.view animated:YES];
        NSString *jobId = self.selectJobModel.jobId;
        NSString *regionId = self.selectCityModel.regionId;
        NSMutableArray *industryArr = [[NSMutableArray alloc] init];
        for (IndustryModel *indus in self.industries) {
//            [industryArr addObject:indus.industryId];
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            dic[@"industryId"] = indus.industryId;
            [industryArr addObject:dic];
        }
        
        [SendRequest updateIntentionalPositionWithIntentionalId:@"" andJobId:jobId andRegion:regionId andMinSalary:self.minSalary andMaxSalary:self.maxSalary andIndustries:industryArr result:^(NSDictionary *result, NSError *error) {
            [weakSelf hideProgress:self.view animated:YES];
            result = [CommonUtils decryptDicWithResult:result];
            if([result[@"result"] integerValue] == 1) {
                SaintiLog(@"新增成功");
                [[NSNotificationCenter defaultCenter] postNotificationName:SHIRefreshIntentionalList object:nil];
                weakSelf.addBlock();
                [weakSelf.navigationController popViewControllerAnimated:YES];
            } else {
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
        }];
    }
}

- (IBAction)editCompleteBtnClick:(id)sender {
    if ([self checkSelect]) {
        WS(weakSelf)
        [self showProgressWithView:self.view animated:YES];
        NSString *industryId = self.model.intentionalId;
        NSString *jobId = self.selectJobModel.jobId;
        NSString *regionId = self.selectCityModel.regionId;
        NSMutableArray *industryArr = [[NSMutableArray alloc] init];
        for (IndustryModel *indus in self.industries) {
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            dic[@"industryId"] = indus.industryId;
            [industryArr addObject:dic];
        }
        
        [SendRequest updateIntentionalPositionWithIntentionalId:industryId andJobId:jobId andRegion:regionId andMinSalary:self.minSalary andMaxSalary:self.maxSalary andIndustries:industryArr result:^(NSDictionary *result, NSError *error) {
            [weakSelf hideProgress:self.view animated:YES];
            result = [CommonUtils decryptDicWithResult:result];
            if ([result[@"result"] integerValue] == 1) {
                SaintiLog(@"更新成功");
                weakSelf.addBlock();
                [weakSelf.navigationController popViewControllerAnimated:YES];
            } else {
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
        }];
    }
}

- (IBAction)editDeleteBtnClick:(id)sender {
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    NSString *industryId = self.model.intentionalId;
    [SendRequest deleteIntentionalPositionWithIntentionalId:industryId result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if ([result[@"result"] integerValue] == 1) {
            SaintiLog(@"删除成功");
            [[NSNotificationCenter defaultCenter] postNotificationName:SHIRefreshIntentionalList object:nil];
            weakSelf.addBlock();
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (IBAction)jobBtnClick:(id)sender {
    PositionSelectViewController *select = [[PositionSelectViewController alloc] init];
    
    WS(weakSelf)
    select.selectBlock = ^(JobModel *job) {
        weakSelf.selectJobModel = job;
        weakSelf.jobL.text = job.jobName;
    };
    [self.navigationController pushViewController:select animated:YES];
}

- (IBAction)industryBtnClick:(id)sender {
    ExpectedIndustryVC *eiVC = [[ExpectedIndustryVC alloc] init];
    eiVC.jobs = self.industries;
    eiVC.industryBlock = ^(NSArray *dataArray) {
        self.industries = dataArray;
        [self.model.industries removeAllObjects];
        [self.model.industries addObjectsFromArray:dataArray];
        self.industryL.text = [NSString stringWithFormat:@"%zd个标签", self.industries.count];
    };
    [self.navigationController pushViewController:eiVC animated:YES];
}

- (IBAction)addressBtnClick:(id)sender {
    SelectCityViewController *scVC = [[SelectCityViewController alloc] init];
    WS(weakSelf)
    scVC.cityBlock = ^(CityModel *city) {
        weakSelf.selectCityModel = city;
        weakSelf.addressL.text = city.regionName;
    };
//    [self.navigationController pushViewController:scVC animated:YES];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:scVC];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)SearchCityBlock:(NSNotification *)notification {
    // 得到传递的参数
    NSDictionary *dict = [notification userInfo];
    // 重新选择收货地址后，走此条件
    CityModel *city = [dict objectForKey:@"city"];
    self.selectCityModel = city;
    self.addressL.text = city.regionName;
}

- (IBAction)salaryBtnClick:(id)sender {
    SalarySelectViewController *salary = [[SalarySelectViewController alloc] init];
    WS(weakSelf)
    salary.selectBlock = ^(NSString *minSalary, NSString *maxSalary) {
        weakSelf.minSalary = [minSalary substringToIndex:minSalary.length - 1];
        weakSelf.maxSalary = [maxSalary substringToIndex:maxSalary.length - 1];
        weakSelf.salaryL.text = [NSString stringWithFormat:@"%@-%@", minSalary, maxSalary];
    };
    // 核心代码
    self.definesPresentationContext = YES;
    salary.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self.navigationController presentViewController:salary animated:NO completion:nil];
}

//把多个json字符串转为一个json字符串
- (NSString *)objArrayToJSON:(NSArray *)array {
    NSString *jsonStr = @"[";
    for (NSInteger i = 0; i < array.count; ++i) {
        if (i != 0) {
            jsonStr = [jsonStr stringByAppendingString:@","];
        }
        jsonStr = [jsonStr stringByAppendingString:array[i]];
    }
    jsonStr = [jsonStr stringByAppendingString:@"]"];
    return jsonStr;
}

@end
